load("//bazel:tensorstore.bzl", "tensorstore_cc_binary", "tensorstore_cc_library", "tensorstore_cc_test")
load("//docs:doctest.bzl", "doctest_test")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

DOCTEST_SOURCES = glob([
    "**/*.rst",
    "**/*.yml",
])

doctest_test(
    name = "doctest_test",
    srcs = DOCTEST_SOURCES,
)

filegroup(
    name = "doc_sources",
    srcs = DOCTEST_SOURCES,
)

tensorstore_cc_library(
    name = "downsample",
    srcs = ["downsample.cc"],
    hdrs = ["downsample.h"],
    deps = [
        ":downsample_array",
        ":downsample_method_json_binder",
        ":downsample_nditerable",
        ":downsample_util",
        ":grid_occupancy_map",
        "//tensorstore:array",
        "//tensorstore:array_storage_statistics",
        "//tensorstore:box",
        "//tensorstore:chunk_layout",
        "//tensorstore:codec_spec",
        "//tensorstore:context",
        "//tensorstore:contiguous_layout",
        "//tensorstore:data_type",
        "//tensorstore:downsample_method",
        "//tensorstore:index",
        "//tensorstore:json_serialization_options",
        "//tensorstore:open_mode",
        "//tensorstore:open_options",
        "//tensorstore:rank",
        "//tensorstore:resize_options",
        "//tensorstore:schema",
        "//tensorstore:spec",
        "//tensorstore:transaction",
        "//tensorstore/driver",
        "//tensorstore/driver:chunk",
        "//tensorstore/index_space:dim_expression",
        "//tensorstore/index_space:dimension_units",
        "//tensorstore/index_space:index_transform",
        "//tensorstore/index_space:transformed_array",
        "//tensorstore/internal:arena",
        "//tensorstore/internal:intrusive_ptr",
        "//tensorstore/internal:lock_collection",
        "//tensorstore/internal:nditerable_transformed_array",
        "//tensorstore/internal/json_binding",
        "//tensorstore/kvstore",
        "//tensorstore/serialization",
        "//tensorstore/util:executor",
        "//tensorstore/util:future",
        "//tensorstore/util:iterate",
        "//tensorstore/util:result",
        "//tensorstore/util:span",
        "//tensorstore/util:status",
        "//tensorstore/util:str_cat",
        "//tensorstore/util/execution",
        "//tensorstore/util/execution:sender_util",
        "//tensorstore/util/garbage_collection",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/container:inlined_vector",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/synchronization",
    ],
    alwayslink = True,
)

tensorstore_cc_library(
    name = "downsample_array",
    srcs = ["downsample_array.cc"],
    hdrs = ["downsample_array.h"],
    deps = [
        ":downsample_nditerable",
        ":downsample_util",
        "//tensorstore:array",
        "//tensorstore:downsample_method",
        "//tensorstore:index",
        "//tensorstore/index_space:dim_expression",
        "//tensorstore/index_space:transformed_array",
        "//tensorstore/internal:arena",
        "//tensorstore/internal:nditerable",
        "//tensorstore/internal:nditerable_array",
        "//tensorstore/internal:nditerable_copy",
        "//tensorstore/internal:nditerable_transformed_array",
        "//tensorstore/util:result",
        "//tensorstore/util:span",
        "//tensorstore/util:status",
        "@abseil-cpp//absl/status",
    ],
)

tensorstore_cc_test(
    name = "downsample_array_test",
    size = "small",
    srcs = ["downsample_array_test.cc"],
    deps = [
        ":downsample_array",
        "//tensorstore:array",
        "//tensorstore:array_testutil",
        "//tensorstore:data_type",
        "//tensorstore:downsample_method",
        "//tensorstore:index",
        "//tensorstore/index_space:dim_expression",
        "//tensorstore/index_space:transformed_array",
        "//tensorstore/util:span",
        "@googletest//:gtest_main",
        "@nlohmann_json//:json",
    ],
)

tensorstore_cc_library(
    name = "downsample_nditerable",
    srcs = ["downsample_nditerable.cc"],
    hdrs = ["downsample_nditerable.h"],
    deps = [
        "//tensorstore:box",
        "//tensorstore:data_type",
        "//tensorstore:downsample_method",
        "//tensorstore:index",
        "//tensorstore:rank",
        "//tensorstore/internal:arena",
        "//tensorstore/internal:elementwise_function",
        "//tensorstore/internal:nditerable",
        "//tensorstore/internal:nditerable_buffer_management",
        "//tensorstore/internal:unique_with_intrusive_allocator",
        "//tensorstore/util:extents",
        "//tensorstore/util:iterate",
        "//tensorstore/util:span",
        "//tensorstore/util:str_cat",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/numeric:int128",
        "@abseil-cpp//absl/status",
        "@nlohmann_json//:json",
    ],
)

tensorstore_cc_binary(
    name = "downsample_benchmark_test",
    testonly = 1,
    srcs = ["downsample_benchmark_test.cc"],
    tags = ["benchmark"],
    deps = [
        ":downsample_array",
        ":downsample_nditerable",
        ":downsample_util",
        "//tensorstore:array",
        "//tensorstore:box",
        "//tensorstore:data_type",
        "//tensorstore:index",
        "//tensorstore/internal:data_type_random_generator",
        "//tensorstore/internal:global_initializer",
        "//tensorstore/util:str_cat",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/random",
        "@google_benchmark//:benchmark_main",
    ],
)

tensorstore_cc_test(
    name = "downsample_test",
    size = "small",
    srcs = ["downsample_test.cc"],
    deps = [
        ":downsample",
        "//tensorstore",
        "//tensorstore:array",
        "//tensorstore:box",
        "//tensorstore:chunk_layout",
        "//tensorstore:context",
        "//tensorstore:data_type",
        "//tensorstore:downsample",
        "//tensorstore:downsample_method",
        "//tensorstore:index",
        "//tensorstore:open",
        "//tensorstore:open_mode",
        "//tensorstore:schema",
        "//tensorstore:spec",
        "//tensorstore:static_cast",
        "//tensorstore/driver",
        "//tensorstore/driver:driver_testutil",
        "//tensorstore/driver/array",
        "//tensorstore/driver/cast",
        "//tensorstore/driver/n5",
        "//tensorstore/driver/zarr",
        "//tensorstore/index_space:dim_expression",
        "//tensorstore/index_space:dimension_units",
        "//tensorstore/index_space:index_transform",
        "//tensorstore/index_space:transformed_array",
        "//tensorstore/internal:global_initializer",
        "//tensorstore/internal/testing:json_gtest",
        "//tensorstore/kvstore/memory",
        "//tensorstore/util:status_testutil",
        "//tensorstore/util:str_cat",
        "//tensorstore/util:unit",
        "//tensorstore/util/execution",
        "//tensorstore/util/execution:sender_util",
        "@abseil-cpp//absl/status",
        "@googletest//:gtest_main",
        "@nlohmann_json//:json",
    ],
)

tensorstore_cc_library(
    name = "downsample_util",
    srcs = ["downsample_util.cc"],
    hdrs = ["downsample_util.h"],
    deps = [
        "//tensorstore:array",
        "//tensorstore:box",
        "//tensorstore:downsample_method",
        "//tensorstore:index",
        "//tensorstore:index_interval",
        "//tensorstore:rank",
        "//tensorstore/index_space:index_transform",
        "//tensorstore/index_space:output_index_method",
        "//tensorstore/internal:integer_overflow",
        "//tensorstore/util:byte_strided_pointer",
        "//tensorstore/util:division",
        "//tensorstore/util:iterate",
        "//tensorstore/util:result",
        "//tensorstore/util:span",
        "//tensorstore/util:status",
        "//tensorstore/util:str_cat",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/container:inlined_vector",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/strings",
    ],
)

tensorstore_cc_test(
    name = "downsample_util_test",
    size = "small",
    srcs = ["downsample_util_test.cc"],
    deps = [
        ":downsample_array",
        ":downsample_util",
        "//tensorstore:array",
        "//tensorstore:box",
        "//tensorstore:data_type",
        "//tensorstore:downsample_method",
        "//tensorstore:index",
        "//tensorstore:index_interval",
        "//tensorstore/index_space:dim_expression",
        "//tensorstore/index_space:dimension_identifier",
        "//tensorstore/index_space:index_transform",
        "//tensorstore/index_space:index_transform_testutil",
        "//tensorstore/index_space:transformed_array",
        "//tensorstore/internal:data_type_random_generator",
        "//tensorstore/internal/testing:random_seed",
        "//tensorstore/util:span",
        "//tensorstore/util:status_testutil",
        "//tensorstore/util:str_cat",
        "@abseil-cpp//absl/random",
        "@abseil-cpp//absl/random:bit_gen_ref",
        "@abseil-cpp//absl/status",
        "@googletest//:gtest_main",
    ],
)

tensorstore_cc_library(
    name = "downsample_method_json_binder",
    hdrs = ["downsample_method_json_binder.h"],
    deps = [
        "//tensorstore:downsample_method",
        "//tensorstore/internal/json_binding",
        "//tensorstore/internal/json_binding:bindable",
    ],
)

tensorstore_cc_library(
    name = "grid_occupancy_map",
    srcs = ["grid_occupancy_map.cc"],
    hdrs = ["grid_occupancy_map.h"],
    deps = [
        "//tensorstore:array",
        "//tensorstore:box",
        "//tensorstore:contiguous_layout",
        "//tensorstore:data_type",
        "//tensorstore:index",
        "//tensorstore:index_interval",
        "//tensorstore:rank",
        "//tensorstore/util:iterate",
        "//tensorstore/util:span",
        "@abseil-cpp//absl/container:flat_hash_map",
    ],
)

tensorstore_cc_test(
    name = "grid_occupancy_map_test",
    size = "small",
    srcs = ["grid_occupancy_map_test.cc"],
    deps = [
        ":grid_occupancy_map",
        "//tensorstore:box",
        "//tensorstore:index",
        "@googletest//:gtest_main",
    ],
)
